package com.fudan.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fudan.ext.entities.User;
import com.fudan.tool.DozerUtil;

@Repository
public class UserDaoImpl {
	
	@Autowired
	private UserRepository userRepostory;
	
	
	public List<com.fudan.dto.entities.User> getAllUser(){
		List<User> users = userRepostory.findAll();
		return DozerUtil.transforList(users, com.fudan.dto.entities.User.class);	
		
	}
	
	public com.fudan.dto.entities.User getUserbyId(Integer userId) {
		Optional<User> user = userRepostory.findById(userId);
		return DozerUtil.transfor(user.get(), com.fudan.dto.entities.User.class);	
	}
	
	public List<com.fudan.dto.entities.User> getUserByUsername(String username){
		List<User> users = userRepostory.findByUsername(username);
		
		return DozerUtil.transforList(users, com.fudan.dto.entities.User.class);	
	}
	
	public com.fudan.dto.entities.User createUser(String username, String password, String role) {
		User user = new User(username, password, role);
		
		user = userRepostory.save(user);
		return DozerUtil.transfor(user, com.fudan.dto.entities.User.class);
	}
	

}
